package com.atguigu.servlet;

import com.atguigu.bean.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Date:2020/9/9
 * Author:ybc
 * Description:
 */
public class UserServlet extends BaseServlet {

    /**
     * 实现登录功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //显示业务逻辑的处理，即调用service
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);
        //判断通过用户名和密码查询的用户对象是否为null
        if(user == null){
            //将错误提示信息在请求域中进行共享
            request.setAttribute("errorMsg", "用户名或密码错误");
            //通过用户名和密码查询的用户对象为null，即登录失败，需要使用转发跳转到登录页面
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else{
            //将登录成功的用户信息在session中进行共享
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            //通过用户名和密码查询的用户对象不为null，即登录成功，需要使用重定向跳转到登录成功页面
            response.sendRedirect(request.getContextPath() + "/pages/user/login_success.jsp");
        }
    }

    /**
     * 实现注册功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        //处理业务逻辑，即调用service
        UserService userService = new UserServiceImpl();
        //将从浏览器端收集的用户信息封装到User对象中
        User user = new User(null, username, password, email);
        boolean result = userService.regist(user);
        //判断注册的结果
        if(result){
            //表示注册成功，需要使用重定向跳转到注册成功页面
            response.sendRedirect(request.getContextPath() + "/pages/user/regist_success.jsp");
        }else{
            //将错误提示信息在请求域中进行共享
            request.setAttribute("errorMsg", "用户名已经被注册");
            //表示注册失败，需要通过转发跳转到原页面即注册页面
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }

    /**
     * 注销（登出、退出）
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //将session中共享的用户信息删除
        session.removeAttribute("user");
        //重定向到主页面
        response.sendRedirect(request.getContextPath());
    }
}
